package com.lb.activity.sys.controller;

import com.lb.activity.po.Permission;
import com.lb.activity.po.Tree;
import com.lb.activity.sys.service.PermissionService;
import com.lb.activity.util.JsonUtil;
import com.lb.activity.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单
 * @author 001030
 */
@RestController
@Slf4j
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 菜单页面
     * @return
     */
    @GetMapping("permission")
    public ModelAndView primission(){
        return new ModelAndView("permission/menus");
    }

    /**
     * 图标
     * @return
     */
    @GetMapping("icons")
    public ModelAndView fontIcoList(){
        return  new ModelAndView("permission/icons");
    }


    /**
     * 菜单列表
     * @return
     */
    @GetMapping("menus")
    public Object menus(){
        List<Permission> adminPermissions= permissionService.AllMeuns();
        String json="";
        try {
            json = JsonUtil.objectToJson(adminPermissions).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        json=json.replaceAll("id","menuId").replaceAll("parentmenuId","parentId");
        return json;
    }

    /**
     * 添加菜单
     * @param permission
     * @return
     */
    @PostMapping("addMenu")
    public ModelAndView addMenu(Permission permission){
        permissionService.addMenu(permission);
        return new ModelAndView("redirect:/permission");
    }

    /**
     * 根据菜单名称判断是否重复添加
     * @param permission
     * @return
     */
    @PostMapping("getMenusByName")
    public Object getMenusByName(Permission permission){
        List<Permission> storeAdmins = permissionService.getMenusByName(permission);
        if(storeAdmins!=null&&storeAdmins.size()!=0){
            return ResultUtil.build(-1, "");
        }
        return ResultUtil.ok();
    }

    /**
     * 根据ID 查询
     * @param id
     * @return
     */
    @PostMapping("getMenusById")
    public Object getMenusById(Long id){
        String json="";
        try {
            Permission storeAdminPermission =permissionService.getPermission(id);
            if(storeAdminPermission!=null&&storeAdminPermission.getParentid()==0){
                storeAdminPermission.setBei1("根目录");
            }else{
                Permission parent = permissionService.getPermission(storeAdminPermission.getParentid());
                if(parent!= null){
                    storeAdminPermission.setBei1(parent.getName());
                }
            }
            json = JsonUtil.objectToJson(storeAdminPermission);
        } catch (IOException e) {
            log.info(e.getMessage() +"getMenusById");
        }
        return json;
    }


    @PostMapping("removeMenuById")
    public Object removeMenuById(Long id){
        try {
            List<Permission> storeAdmins = permissionService.getMenusByPid(id);
            if(storeAdmins!=null&storeAdmins.size()!=0){
                return ResultUtil.build(-1,"该菜单包含下级菜单,请先删除下级菜单");
            }
            permissionService.delete(id);
        } catch (Exception e) {
            log.info(e.getMessage()+"/delete");
        }
        return ResultUtil.ok();
    }


    @PostMapping("menuTree")
    public Tree<Permission> menuTree(Long roleId) {
        Tree<Permission> tree = permissionService.getTree(roleId);
        return tree;
    }

    @PostMapping("modifyRoleMenus")
    public Object modifyRoleMenus(Long id,String[] menuIds){
        try {
            //String [] menuIdss = request.getParameterValues("menuIds");
            List<String> menus=new ArrayList<String>();
            if(menuIds!=null){
                menus = Arrays.asList(menuIds);
            }
            permissionService.updateRoleAndmenuIds(id,menus);
        } catch (Exception e) {
            log.info(e.getMessage()+"/update");
            return ResultUtil.build(-1,"分配权限失败");
        }
        return ResultUtil.ok();
    }



}
